﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Domain;
using S4Store.Service;
using S4Store.Models.Report;
using S4Store.Models;

namespace S4Store.Controllers
{
    public class ReportBaseController : BaseController
    {

        protected Store Store;
        protected DateTime Month = DateTime.Now;
        protected DateTime? Start = null;
        protected DateTime? End = null;
        protected int? SearchSellerId { get; set; }
        protected int? SearchCarBrandId { get; set; }
        protected int? SearchCarModelId { get; set; }

        public StoreService StoreService { get; set; }
        protected ReportBarModel BarModel { get; set; }


        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            BarModel = new ReportBarModel();
            using (var t = new Transtion())
            {
                t.Inject<StoreService>(StoreService);
                if (!StoreService.GetQuery().Any())
                {
                    throw new Exception("请先添加门店");
                }
                t.Commit();
            }

            if (Request["StoreId"] != null)
            {
                int storeid = int.Parse(Request["StoreId"]);
                HttpCookie cookiestoreid = new HttpCookie("StoreId", storeid.ToString());
                Response.Cookies.Add(cookiestoreid);
                using (var t = new Transtion())
                {
                    t.Inject<StoreService>(StoreService);
                    Store = StoreService.GetQuery().FirstOrDefault(x => x.Id == storeid);
                    t.Commit();
                }
            }
            else if (CurrentUser.StoreId != null && CurrentUser.StoreId.Value != 0)
            {
                Store = CurrentUser.Store;
            }
            else
            {
                using (var t = new Transtion())
                {
                    t.Inject<StoreService>(StoreService);
                    Store = StoreService.GetQuery().FirstOrDefault();
                    t.Commit();
                }
            }
            if (Request["Month"] != null)
            {
                Month = DateTime.Parse(Request["Month"]);
                HttpCookie cookiemonth=new HttpCookie("Month",Month.ToString("yyyy-MM-01"));
                Response.Cookies.Add(cookiemonth);
            }
            if (Request["Start"] != null)
            {
                Start = DateTime.Parse(Request["Start"]);
            }
            if (Request["End"] != null)
            {
                End = DateTime.Parse(Request["End"]);
            }
            if (Request["SearchSellerId"] != null)
            {
                SearchSellerId = int.Parse(Request["SearchSellerId"]);
            }
            if (Request["SearchCarBrandId"] != null)
            {
                SearchCarBrandId = int.Parse(Request["SearchCarBrandId"]);
            }
            if (Request["SearchCarModelId"] != null)
            {
                SearchCarModelId = int.Parse(Request["SearchCarModelId"]);
            }
            BarModel.Stores = this.GetStores();
            BarModel.Store = Store;
            BarModel.Start = Start;
            BarModel.End = End;
            BarModel.Month = Month;
            BarModel.SearchCarModelId = SearchCarModelId;
            BarModel.SearchCarBrandId = SearchCarBrandId;
            BarModel.SearchSellerId = SearchSellerId;
            if (CurrentUser.Position == Position.GeneralManager ||
                CurrentUser.Position == Position.SystemManager)
            {
                BarModel.AddView(ReportBarModel.ViewItem.Stores);
            }
        }


        public List<Store> GetStores()
        {
            using (var t = new Transtion())
            {
                t.Inject<StoreService>(StoreService);
                var stores = StoreService.GetQuery().ToList();
                t.Commit();
                return stores;
            } 
        }

        public void InjectReportModel(BaseReportModel model)
        {
            model.BarModel.Stores = this.GetStores();
            model.BarModel.Store = Store;
            model.BarModel.Start = Start;
            model.BarModel.End = End;
            model.BarModel.Month = Month;
            model.BarModel.SearchCarModelId = SearchCarModelId;
            model.BarModel.SearchCarBrandId = SearchCarBrandId;
            model.BarModel.SearchSellerId = SearchSellerId;
            if (CurrentUser.Position == Position.GeneralManager ||
                CurrentUser.Position == Position.SystemManager)
            {
                model.BarModel.AddView(ReportBarModel.ViewItem.Stores);
            }
        }
    
    }
}
